<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>cookie</title>

    <script type="text/javascript">
    //<![CDATA[
        
        window.onload = function() {
            if(navigator.cookieEnabled) {
                var sum = readCookie("sum");
                if(sum) {
                    var iSum = parseInt(sum) + 1;
                    alert("cookie count is " + iSum);

                    if(iSum > 5) {
                        eraseCookie("sum");
                    }
                    else {
                        setCookie("sum", iSum);
                    }
                }
                else {
                    alert("no cookie, setting now");
                    setCookie("sum", 0);
                }
            }
        }
    
        function setCookie(key, value) {
            var cookieDate = new Date(2019, 10, 10, 19, 30 ,30);
            document.cookie = key + "=" + encodeURI(value) +"; expires=" + cookieDate.toGMTString() + "; path=/";

        }

        function readCookie(key) {
            var cookie = document.cookie;
            var first = cookie.indexOf(key+"=");

            if(first >= 0) {
                var str = cookie.substring(first, cookie.length);
                var last = str.indexOf(";");

                if(last < 0) {
                    last = str.length;
                }

                str = str.substring(0, last).split("=");
                return decodeURI(str[1]);
            }
            else {
                return null;
            }

            function eraseCookie(key) {
                var cookieDate = new Date(2000, 11, 10, 19, 30 , 30);
                document.cookie = key + "=; expires=" + cookieDate.toGMTString()+"; path=/";
            }
        }

    //]]>

    </script>
    
</head>
<body>
    <p>Paragraph text.</p>
</body>
</html>
